import React from 'react';
import { SheetViewer, DocxViewer } from 'react-office-viewer';
import ByteMD from '../components/byteMD';
import CodeEditor from '../components/codeeditor';
import AudioPlayer from '../components/audio/audio';
import { Image } from 'antd';
import { JolPlayer } from 'jol-player';
import {FileItemShare} from '../components/audio/index'
import FileWps from '../components/fileWps/index'
import 'devextreme/dist/css/dx.common.css';
import 'devextreme/dist/css/dx.light.css';
import { loadMessages, locale } from 'devextreme/localization';
import config from 'devextreme/core/config';
config({ defaultCurrency: 'zh' });
import zhMessage from 'devextreme/localization/messages/zh.json';
import DicomViewer from '../components/dicomViewer'
loadMessages(zhMessage);
locale('zh');
interface IProps {
  type: string;//类型
  name: string;//文件名称
  link: string;//文件链接
  contentType?: string;//视频格式（视频必填）
  poster?: string;//视频封面（视频必填）
  shareFile?:FileItemShare//文件信息(音频必填)
  wps?:boolean;//是否使用wps打开
  // requestType?:"wadouri" | "wadors" | "dicomweb" | "dicomfile";//dcm打开方式
  finished?: () => void;//关闭回调
  getHtml?: (html: any) => void;//md保存数据
  
}
/** 实体文件综合 */
const EntityFiles: React.FC<IProps> = ({ type,name,link,poster,contentType,shareFile,wps=false,finished,getHtml }) => {
  const LoadViewer = () => {
    const config = {
      locale: 'zh',
      timeout: 5000,
      fileName: name,
      file: link,
    };
    switch (type) {
      case '.xls':
      case '.xlsx':
        return <SheetViewer {...config} />;
      case '.docx':
        return <DocxViewer {...config} />;
      case '.pdf':
        return (
          <iframe
            width={'100%'}
            height={'100%'}
            loading="eager"
            name={name}
            src={link}
          />
        );
        case '.md':
          return <ByteMD link={link} getHtml={getHtml!} />;
        case '.vue':
        case '.tsx':
        case '.ts':
        case '.jsx':
        case '.js':
        case '.json':
        case '.html':
        case '.java':
          return  <CodeEditor link={link} type={type} name={name} />;
        case '.image':
          return  <Image
          style={{ display: 'none' }}
          src={link}
          preview={{
            visible: true,
            destroyOnClose: true,
            onVisibleChange: (v) => {
              if (!v) {
                finished && finished();
              }
            },
          }}
        />;
        case '.video':
          return  <JolPlayer
          option={{
            width: 830,
            height: 480,
            language: 'zh',
            pausePlacement: 'center',
            isShowScreenshot: false,
            poster: poster,
            videoSrc: link,
            videoType: contentType === 'video/stream' ? 'hls' : 'h264',
          }}
        />;
        case '.audio':
          return  <AudioPlayer finished={finished!} link={link} share={shareFile!}/>;
          case '.dcm':
          return <DicomViewer imageId={'wadouri'+':'+link}/>
    }
    return <></>;
  };
  const LoadViewerWps = () => {
    if(type == '.image'){
      return  <Image
      style={{ display: 'none' }}
      src={link}
      preview={{
        visible: true,
        destroyOnClose: true,
        onVisibleChange: (v) => {
          if (!v) {
            finished && finished();
          }
        },
      }}
    />;
    }else if(type == '.video'){
      return  <JolPlayer
          option={{
            width: 830,
            height: 480,
            language: 'zh',
            pausePlacement: 'center',
            isShowScreenshot: false,
            poster: poster,
            videoSrc: link,
            videoType: contentType === 'video/stream' ? 'hls' : 'h264',
          }}
        />;
    }else if (type == '.audio'){
      return  <AudioPlayer finished={finished!} link={link} share={shareFile!}/>;
    }else{
      //wps
      return <FileWps link={link}/>
    }
    return <></>;
  }
  return (
    <>
        {wps==false && <LoadViewer/>}
        {wps==true && <LoadViewerWps/>}
        </>
  )
};
export default EntityFiles;